﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using OF.Microservices.DemoService.Comon;
using OF.Microservices.DemoService.Entity;


namespace OF.Microservices.DemoService.Utils
{
    /// <summary>
    /// 验证帮助类，将根据对 System.ComponentModel.DataAnnotations 声明的自定义属性进行验证！
    /// </summary>
    public static class ValidatorHelper
    {
        /// <summary>
        /// 验证器对象
        /// </summary>
        /// <param name="instance"></param>
        /// <exception cref="BusinessException">抛出验证出现的错误</exception>
        public static void ValidateObject(object instance)
        {
            List<ValidationResult> validationResults = new List<ValidationResult>();
            if (!Validator.TryValidateObject(instance, new ValidationContext(instance, null, null), validationResults,
                true))
            {
                throw new BusinessException(String.Join(Environment.NewLine, validationResults));
            }
        }
    }
}
